var utils=require('../utils')
var router = require('express').Router()
var sql=require('../sql')
var Banner=require('../sql/models/Banner')

/**
 * @api {get} /api/banner/ 获取轮播图
 * @apiName GetList
 * @apiGroup Banner
 *
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} message  描述
 * @apiSuccess {String} data  数据
 *
 * @apiHeader {String} token token
 * 
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": "200",
 *       "message": "获取轮播图",
 *       "data": []
 *     }
 * @apiSampleRequest /api/banner/
 */
router.get('/',(req,res,next)=>{
    sql.find(Banner,{},{_id:0,_v:0}).then(data=>{
        res.status('200').send({
            code: '200',
            message: '获取轮播图',
            data
          })
    })
    
})
/**
 * @api {post} /api/banner/add 添加轮播图
 * @apiName addBanner
 * @apiGroup Banner
 * 
 * @apiParam {Number} id User's unique ID.
 * 
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} message  描述
 * @apiSuccess {String} data  数据
 * 
 * @apiHeader {String} token token
 * 
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": "200",
 *       "message": "添加轮播图",
 *       "data": []
 *     }
 * @apiSampleRequest /api/banner/add
 */
router.post('/add',(req,res,next)=>{
    var insertData=req.body
     
    insertData.bannerid='banner_'+utils.getUuid()
    sql.insert(Banner,insertData).then(()=>{
        res.status('200').send({
            code: '200',
            message: '添加轮播图'
          })
    })
   
})
/**
 * @api {get} /api/banner/delete 删除轮播图
 * @apiName deleteBanner
 * @apiGroup Banner
 * 
 *@apiParam {String} bannerid 要删除的轮播图的id

 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} message  描述
 * @apiSuccess {String} data  数据
 *
 * @apiHeader {String} token token
 * 
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": "200",
 *       "message": "删除轮播图",
 *       "data": []
 *     }
 * @apiSampleRequest /api/banner/delete
 */
router.get('/delete',(req,res,next)=>{
    var {bannerid}=req.query
    
    sql.delete(Banner,{bannerid}).then((data)=>{
        res.status('200').send({
            code: '200',
            message: '删除单张轮播图'
          })
    })
})
/**
 * @api {get} /api/banner/deleteAll 删除所有轮播图
 * @apiName deleteAllBanner
 * @apiGroup Banner
 *
 * @apiParam {String} bannerid 要删除的轮播图的id
 * 
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} message  描述
 * @apiSuccess {String} data  数据
 *
 * @apiHeader {String} token token
 * 
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": "200",
 *       "message": "删除所有轮播图",
 *       "data": []
 *     }
 * @apiSampleRequest /api/banner/deleteAll
 */
router.get('/deleteAll',(req,res,next)=>{
    var data= req.query.selectedRowKeys
    const arr=[]
    data.forEach((item)=>{
        arr.push(sql.delete(Banner,{bannerid:item}))
    })
    Promise.all(arr).then((data)=>{
        res.status('200').send({
            code: '200',
            message: '删除所有轮播图'
          })
    })
    
})

module.exports=router